package com.swak.im.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotatedElementUtils;

import com.swak.annotation.ImOps;
import com.swak.annotation.im.DistributedIm;
import com.swak.im.ImService;
import com.swak.im.ImWorker;
import com.swak.security.Subject;
import com.swak.vertx.config.ImBean;
import com.swak.vertx.invoker.MethodInvoker;
import com.swak.vertx.protocol.im.ImContext;

import lombok.Getter;
import lombok.Setter;

/**
 * 分布式IM
 * 
 * @author 618lf
 */
@Getter
@Setter
public class DistributedImBean extends ImBean {

	@Autowired
	private ImService imService;
	private ImWorker imWorker;

	@Override
	public void initializing() throws Exception {

		// 定义 im 的处理
		DistributedIm im = AnnotatedElementUtils.findMergedAnnotation(type, DistributedIm.class);

		// 支持的路径
		this.path = im.path();

		// 端口
		this.port = im.port();

		// 服务个数
		this.instances = im.parallel();

		// method
		this.ops = ImOps.All;

		// worker
		this.imWorker = this.imService.createWorker(im, ref);

		// worker -> handle
		this.method = ImWorker.class.getMethod("handle", new Class[] { ImContext.class, Subject.class });

		// 执行器
		this.methodInvoker = new MethodInvoker(ImWorker.class, this.imWorker, method);
	}
}